fun encrypt(x:int,c:char) = 
	if (ord c) mod 2 = 0 then x + 1
    	else x - 1;
        
fun hex2decchar(c:char) = 
	if (c >= #"0" andalso c <= #"9") then (ord c) - ord(#"0")
    else if (c >= #"A" andalso c <= #"F") then (ord c) - ord(#"A") + 10
    	else ~1;
        
fun hex2dec(s:string) =
let
	val n = size(s)
in
	if (n = 0)then 0
    else 
    let
    	val prev = hex2dec(String.substring(s,0, n-1))
        val curr = hex2decchar(String.sub(s, n-1))
    in
        if (prev = ~1 orelse curr = ~1) then ~1
        else 
        prev * 16 + curr    
    end     
end
   
 
fun doInvFact(n:int,d:int) = 
if (n = 1) then d - 1
else
 if (n mod d = 0) then doInvFact(n div d,d+1)
 else
  ~1
 
fun invFact(n:int) = 
if (n <= 0) then ~1
else 
	doInvFact(n,2)
    

fun concat c = fn n => if (n<=0) then "" 
else
	((concat c) (n - 1) )^(str c)
    
    
fun legal(f) = fn (k) => fn(m)=>
if (k <= 0) then false
else
	if (f(k)*f(k) = m) then true 
	else
		(((legal f)(k - 1)) m)
        
(* val f = fn : 'a * 'b -> (bool * 'a -> int) -> bool * 'c -> int -> 'a * 'c * 'b *)
fun f(a:'a,b) = 
	fn (g:bool*'a->int) =>
    	fn (tt:bool, c) =>
        	fn (i:int) => (a,c,b) 